package com.dcm.user.service.impl;

import com.dcm.common.constant.Constants;
import com.dcm.common.exception.MyException;
import com.dcm.user.dto.UserDto;
import com.dcm.user.entity.UserEntity;
import com.dcm.user.mapper.UserMapper;
import com.dcm.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * [用户]
 *
 * @author : [pengrongsheng]
 * @createTime : [2021/4/2]
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserEntity> findAll() {
        return userMapper.findAll();
    }

    @Override
    public void register(UserEntity user) {
        if (StringUtils.isBlank(user.getUsername())) {
            throw new MyException(Constants.UserErrorMsg.USERNAME_CANNOT_NULL);
        }
        if (StringUtils.isBlank(user.getPassword())) {
            throw new MyException(Constants.UserErrorMsg.DETAIL_ADDRESS_CANNOT_NULL);
        }
        if (StringUtils.isBlank(user.getPhone())) {
            throw new MyException(Constants.UserErrorMsg.PHONE_CANNOT_NULL);
        }

        userMapper.register(user);
    }

    @Override
    public UserEntity login(UserDto user) {
        if (StringUtils.isBlank(user.getUsername())) {
            throw new MyException(Constants.UserErrorMsg.USERNAME_CANNOT_NULL);
        }
        if (StringUtils.isBlank(user.getPassword())) {
            throw new MyException(Constants.UserErrorMsg.DETAIL_ADDRESS_CANNOT_NULL);
        }
        UserEntity userEntity = userMapper.login(user);

        return userEntity;
    }
}
